<?
session_start();

$config_uri = $_SERVER['DOCUMENT_ROOT'] . "/drizzo/engine/config.php";
@require_once($config_uri);

$functions_db_uri = $_SERVER['DOCUMENT_ROOT'] . "/drizzo/engine/functions_db.php";
@require_once($functions_db_uri);

if(($_POST['install'] == "true") && !file_exists($config_uri)) {
			$http_check = substr($_POST['drizzo_domain'], '0', '7');
			if($http_check == "http://") {
				$drizzo_domain = substr($_POST['drizzo_domain'], '7');
			} else {
				$drizzo_domain = $_POST['drizzo_domain'];
			}
			$configuration = 
	"<?
		/* Database Related */
		define(\"drizzo_db\", \"" . $_POST['db'] . "\");
		define(\"drizzo_db_user\", \"" . $_POST['db_user'] . "\");
		define(\"drizzo_db_pass\", \"" . $_POST['db_pass'] . "\");
		define(\"drizzo_db_host\", \"" . $_POST['db_host'] . "\");
		define(\"drizzo_db_prefix\", \"" . $_POST['db_prefix'] . "\");

		\$drizzo_comments = drizzo_db_prefix . \"comments\";
		\$drizzo_events = drizzo_db_prefix . \"events\";
		\$drizzo_gallery = drizzo_db_prefix . \"gallery\";
		\$drizzo_links = drizzo_db_prefix . \"links\";
		\$drizzo_options = drizzo_db_prefix . \"options\";
		\$drizzo_posts = drizzo_db_prefix . \"posts\";
		\$drizzo_trashheap = drizzo_db_prefix . \"trashheap\";
		\$drizzo_users = drizzo_db_prefix . \"users\";
		\$drizzo_warehouse = drizzo_db_prefix . \"warehouse\";

		/* Site Related */
		define(\"domain_name\", \"" . $drizzo_domain . "\");
		define(\"admin_folder\", \"/drizzo/\");

		\$drizzo_admin_css = admin_folder . \"css/drizzo.css\";
		\$drizzo_admin_images = admin_folder . \"images/\";
		\$drizzo_admin_js = admin_folder . \"js/\";
		\$drizzo_admin_modules = admin_folder . \"modules/\";
		\$drizzo_admin_plugins = admin_folder . \"plugins/\";

	?>";

			$config_uri = $_SERVER['DOCUMENT_ROOT'] . "/drizzo/engine/config.php";
			$config_file = fopen($config_uri, 'w') or die("Please make the '/drizzo/engine' directory writable.</p>");
			fwrite($config_file, $configuration);
			fclose($config_file);
}

function drizzo_install_check() {
	global $drizzo_options;

	if(!file_exists($config_uri)) {
		$is_installed = 0;
	} else {

		drizzo_db_connect();

		$drizzo_install_check = @mysql_query("SELECT * FROM `$drizzo_options` WHERE `name`='install_timestamp'");
		if($drizzo_install_check) {
			$is_installed = @mysql_num_rows($drizzo_install_check);
		} else {
			$is_installed = 0;
		}
	}

	return $is_installed;
}

function install_drizzo() {
	
	global $drizzo_comments, $drizzo_events, $drizzo_gallery, $drizzo_links, $drizzo_options, $drizzo_posts, $drizzo_users, $drizzo_trashheap, $drizzo_warehouse;
	
	$sql_comments = "CREATE TABLE `$drizzo_comments` (
	  `ID` int(11) NOT NULL auto_increment,
	  `post_id` int(11) NOT NULL default '0',
	  `parent_id` int(11) NOT NULL default '0',
	  `date` varchar(14) NOT NULL,
	  `content` longtext NOT NULL,
	  `title` varchar(255) default NULL,
	  `tags` text,
	  `author_id` int(11) default NULL,
	  `author_name` varchar(32) default NULL,
	  `author_email` varchar(100) default NULL,
	  `author_url` varchar(255) default NULL,
	  `author_ip` varchar(24) NOT NULL default '',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `status` enum('moderation','approved','spam') NOT NULL default 'moderation',
	  PRIMARY KEY  (`ID`),
	  FULLTEXT KEY `content` (`content`)
	) TYPE=MyISAM;";

	$sql_events = "CREATE TABLE `$drizzo_events` (
	  `ID` int(11) NOT NULL auto_increment,
	  `when` varchar(14) NOT NULL,
	  `what` longtext NOT NULL,
	  `where` longtext,
	  `who` longtext,
	  `notes` longtext,
	  `tags` longtext,
	  `added_by` int(11) NOT NULL default '0',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";

	$sql_gallery = "CREATE TABLE `$drizzo_gallery` (
	  `ID` int(11) NOT NULL auto_increment,
	  `title` varchar(64) NOT NULL default '',
	  `description` text NOT NULL,
	  `tags` longtext,
	  `file` longblob,
	  `file_size` varchar(16) default NULL,
	  `file_type` varchar(16) default NULL,
	  `thumb` longblob,
	  `thumb_size` varchar(16) default NULL,
	  `thumb_type` varchar(16) default NULL,
	  `auto_thumb` tinyint(2) NOT NULL default '0',
	  `date_added` varchar(14) NOT NULL,
	  `owner` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";

	$sql_links = "CREATE TABLE `$drizzo_links` (
	  `ID` int(11) NOT NULL auto_increment,
	  `name` varchar(64) NOT NULL default '',
	  `url` varchar(100) NOT NULL default '',
	  `tags` longtext NOT NULL,
	  `owner` int(11) NOT NULL default '0',
	  `added` varchar(14) NOT NULL,
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";

	$sql_options = "CREATE TABLE `$drizzo_options` (
	  `ID` int(11) NOT NULL auto_increment,
	  `name` varchar(32) NOT NULL default '',
	  `value` varchar(100) NOT NULL default '',
	  `owner` int(11) default NULL,
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";

	$sql_posts = "CREATE TABLE `$drizzo_posts` (
	  `ID` int(11) NOT NULL auto_increment,
	  `author` int(11) NOT NULL default '0',
	  `type` enum('blog','review','page','netcast','aside') NOT NULL default 'blog',
	  `status` enum('publish','draft','private') NOT NULL default 'publish',
	  `page_parent` int(11) NOT NULL default '0',
	  `page_order` int(11) NOT NULL default '0',
	  `title` longtext NOT NULL,
	  `content` longtext NOT NULL,
	  `tags` longtext,
	  `comment_status` enum('open','closed') NOT NULL default 'open',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `posted_date` varchar(14) NOT NULL,
	  `import_id` int(11) default NULL,
	  `permalink_name` varchar(100) default NULL,
	  `menu_name` varchar(100) default NULL,
	  `lat` varchar(12) default NULL,
	  `lon` varchar(12) default NULL,
	  PRIMARY KEY  (`ID`),
	  FULLTEXT KEY `title` (`title`,`content`)
	) TYPE=MyISAM;";
	
	$sql_trashheap = "CREATE TABLE `$drizzo_trashheap` (
	  `ID` int(11) NOT NULL auto_increment,
	  `author` int(11) NOT NULL default '0',
	  `type` enum('blog','review','page','netcast','aside') NOT NULL default 'blog',
	  `status` enum('publish','draft','private') NOT NULL default 'publish',
	  `page_parent` int(11) default NULL,
	  `page_order` int(11) default NULL,
	  `title` longtext NOT NULL,
	  `content` longtext NOT NULL,
	  `tags` longtext,
	  `comment_status` enum('open','closed') NOT NULL default 'open',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `posted_date` varchar(14) NOT NULL,
	  `permalink_name` varchar(100) default NULL,
	  `menu_name` varchar(100) default NULL,
	  `lat` varchar(12) default NULL,
	  `lon` varchar(12) default NULL,
	  `deleted_by` int(11) NOT NULL default '0',
	  `deleted_date` varchar(14) NOT NULL,
	  `old_id` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`ID`),
	  FULLTEXT KEY `title` (`title`,`content`)
	) TYPE=MyISAM;";


	$sql_users = "CREATE TABLE `$drizzo_users` (
	  `ID` int(11) NOT NULL auto_increment,
	  `first_name` varchar(32) NOT NULL default '',
	  `last_name` varchar(32) NOT NULL default '',
	  `nice_name` varchar(32) default NULL,
	  `gender` varchar(6) default NULL,
	  `url` text,
	  `aim` varchar(64) default NULL,
	  `msn` varchar(64) default NULL,
	  `google_talk` varchar(64) default NULL,
	  `jabber` varchar(64) default NULL,
	  `yahoo` varchar(64) default NULL,
	  `email` varchar(100) default NULL,
	  `user_level` tinyint(2) NOT NULL default '0',
	  `date_added` varchar(14) NOT NULL,
	  `date_edited` varchar(14) NOT NULL,
	  `edited_by` tinyint(4) NOT NULL default '0',
	  `avatar` longblob,
	  `myspace` varchar(32) default NULL,
	  `facebook` varchar(32) default NULL,
	  `digg` varchar(32) default NULL,
	  `lastfm` varchar(32) default NULL,
	  `delicious` varchar(32) default NULL,
	  `about` longtext,
	  `username` varchar(64) NOT NULL default '',
	  `username_md5` varchar(100) default NULL,
	  `password` varchar(100) NOT NULL default '',
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";
	
	$sql_warehouse = "CREATE TABLE `$drizzo_warehouse` (
	  `ID` int(11) NOT NULL auto_increment,
	  `author` int(11) NOT NULL default '0',
	  `type` enum('blog','review','page','netcast','aside') NOT NULL default 'blog',
	  `status` enum('publish','draft','private') NOT NULL default 'publish',
	  `page_parent` int(11) default NULL,
	  `page_order` int(11) default NULL,
	  `title` longtext NOT NULL,
	  `content` longtext NOT NULL,
	  `tags` longtext,
	  `comment_status` enum('open','closed') NOT NULL default 'open',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `posted_date` varchar(14) NOT NULL,
	  `permalink_name` varchar(100) default NULL,
	  `menu_name` varchar(100) default NULL,
	  `lat` varchar(12) default NULL,
	  `lon` varchar(12) default NULL,
	  `old_id` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`ID`),
	  FULLTEXT KEY `title` (`title`,`content`)
	) TYPE=MyISAM;";
	
	$sql_default_options = "INSERT INTO `$drizzo_options` VALUES ('', 'install_timestamp', '" . time() . "', NULL),
	('', 'use_permalinks', '1', NULL),
	('', 'timezone_offset', '0', NULL),
	('', 'dst', '1', NULL),
	('', 'warehouse_limit', '10', NULL),
	('', 'site_name', '" . $_POST['site_name'] . "', NULL),
	('', 'site_description', '" . $_POST['site_tagline'] . "', NULL);";

	drizzo_db_connect();
	
	mysql_query($sql_comments) or die("<p>Comments Table creation failed.<br />" . mysql_error() . "</p>");
	echo "<p>Comments Table creation successful.</p>";

	mysql_query($sql_events) or die("<p>Events Table creation failed." . mysql_error() . "</p>");
	echo "<p>Events Table creation successful.</p>";

	mysql_query($sql_gallery) or die("<p>Gallery Table creation failed." . mysql_error() . "</p>");
	echo "<p>Gallery Table creation successful.</p>";

	mysql_query($sql_links) or die("<p>Links Table creation failed." . mysql_error() . "</p>");
	echo "<p>Links Table creation successful.</p>";

	mysql_query($sql_options) or die("<p>Options Table creation failed." . mysql_error() . "</p>");
	echo "<p>Options Table creation successful.</p>";
	
	mysql_query($sql_default_options) or die("<p>Default Options could not be entered.<br />" . mysql_error() . "</p>");
	echo "<p>Default Options set up.</p>";

	mysql_query($sql_posts) or die("<p>Posts Table creation failed." . mysql_error() . "</p>");
	echo "<p>Posts Table creation successful.</p>";

	mysql_query($sql_trashheap) or die("<p>TrashHeap Table creation failed." . mysql_error() . "</p>");
	echo "<p>TrashHeap Table creation successful.</p>";

	mysql_query($sql_users) or die("<p>Users Table creation failed." . mysql_error() . "</p>");
	echo "<p>Users Table creation successful.</p>";

	mysql_query($sql_warehouse) or die("<p>Warehouse Table creation failed." . mysql_error() . "</p>");
	echo "<p>Warehouse Table creation successful.</p>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		
		<title>Drizzo</title>
		
		<link rel="stylesheet" type="text/css" href="../css/drizzo.css" />

	</head>
	
	<body>
		
		<div id="header">
			
			<div id="header_left"></div>

			<h1 style="display: block; padding: 0.5em 0 0 180px;">Drizzo Installer</h1>
			
			<div id="header_right"></div>

		</div>

		<div id="sidebar">
			<ul>
				<li class="section_header">Installation Progress</li>
<?
	if(($is_installed == 0) && !$_POST) {
		echo "<li><a>1. Database Information</a></li>\n";
	} else {
		echo "<li style=\"color: #aeaeae;\"><a>1. Database Information</a></li>\n";
	}
?>
				<br class="clear" />
			</ul>
		</div>

		<div id="main">
<?
	drizzo_install_check();
	
	if(($is_installed > 0) && !$_POST) {
?>
			<h2>Drizzo Installer</h2>
		
			<p>It appears that Drizzo is already installed.</p>
<?
	}
	
	if(($is_installed == 0) && !$_POST) {
?>
			<h2>Install Drizzo</h2>
			
			<p>
				<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" name="installer" id="installer">
<?
		if(!file_exists($config_uri) || (($_POST['install'] == "true") && (empty($_POST['db']) || empty($_POST['db_user']) || empty($_POST['db_pass']) || empty($_POST['db_host']) || empty($_POST['drizzo_domain'])))) {
			
			if($_POST['install'] == "true") {
				echo "<p>Some necessary options were not filled out. Please try again.</p>\n";
			}
?>
					<label for="db">Database Name <a href="javascript:void(0)">?<span class="explain">Enter the name of the database Drizzo will use.</span></a></label><input type="text" name="db"></input><br />
					<label for="db_host">Database Host <a href="javascript:void(0)">?<span class="explain">Enter the name of the database host. Use 'localhost' if you are unsure.</span></a></label><input type="text" name="db_host" value="localhost"></input><br />
					<label for="db_user">Database Username</label><input type="text" name="db_user"></input><br />
					<label for="db_pass">Database Password</label><input type="password" name="db_pass"></input><br />
					<label for="db_prefix">Database Prefix <a href="javascript:void(0)">?<span class="explain">If you want Drizzo's database tables to have a prefix appended, enter it here. (IE, 'drizzo_' will give you 'drizzo_posts', etc.)</span></a></label><input type="text" name="db_prefix" value="drizzo_"></input><br />
					<label for="drizzo_domain">Drizzo Location <a href="javascript:void(0)">?<span class="explain">Please enter the full URL (www.website.com or www.website.com/blog, etc.) where you want to install Drizzo.</span></a></label><input type="text" name="drizzo_domain" value="<? echo $_SERVER['SERVER_NAME']; ?>"></input><br />
<?
		} else {
			echo "<p>Your configuration file has been found. Please check the box if you would like to import a WordPress blog.  If not, please click \"Start the Install\" to go to the next step.</p>";
		}
?>
					<label for="site_name">Site Name <a href="javascript:void(0)">?<span class="explain">What you want to appear as the title of your site.</span></a></label><input type="text" name="site_name"></input><br />
					<label for="site_tagline">Site Tagline <a href="javascript:void(0)">?<span class="explain">What you want to appear as description/tagline/motto of your site.  You can leave this blank.</span></a></label><input type="text" name="site_tagline"></input><br />
					<label for="import_wordpress">Import WordPress <a href="javascript:void(0)">?<span class="explain">If you currently have a WordPress blog on this server, check this option to be able to import its contents in step 2.</span></a></label><input type="checkbox" class="checkbox" name="import_wordpress"></input><br />
					<input type="hidden" name="install" value="true"></input>
					<input type="submit" name="submit" class="submit" value="Start the Install" onclick="document.getElementById('submit').value = 'Installing...'"></input>
				</form>
			</p>
<?
	}

	if($_POST['install'] == "true") {
?>
	<h2>Drizzo Installation Progess</h2>
<?
		install_drizzo();
		
		if($_POST['import_wordpress'] == 'on') {
?>	
	<p>&nbsp;</p>

	<h2>Import WordPress Data</h2>

	<p>
		<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" name="installer" id="installer" class="wp">
			<label for="wp_db">WordPress Database Name <a href="javascript:void(0)">?<span class="explain">Enter the name of the database WordPress used.</span></a></label><input type="text" name="wp_db"></input><br />
			<label for="wp_db_host">WordPress Database Host <a href="javascript:void(0)">?<span class="explain">Enter the name of the WordPress database host. Use 'localhost' if you are unsure.</span></a></label><input type="text" name="wp_db_host" value="localhost"></input><br />
			<label for="wp_db_user">WordPress Database Username</label><input type="text" name="wp_db_user"></input><br />
			<label for="wp_db_pass">WordPress Database Password</label><input type="password" name="wp_db_pass"></input><br />
			<label for="wp_db_prefix">WordPress Database Prefix <a href="javascript:void(0)">?<span class="explain">If WordPress used a table prefix, enter it here. (WordPress uses wp_ by default.)</span></a></label><input type="text" name="wp_db_prefix" value="wp_"></input><br />
			<input type="hidden" name="wp_import" value="true"></input>
			<input type="submit" name="submit" class="submit"  onclick="document.getElementById('submit').value = 'Importing...'" value="Import WordPress Data"></input>
		</form>
	</p>
	<p>Please only click "Import WordPress Data" once. Importing <em>will</em> take a few minutes.</p>
<?
		} else {
?>
	<p>&nbsp;</p>

	<h2>Create Administrator Account</h2>
	
	<p>
		<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" name="make_admin" id="installer">
			<label for="username">Username</label><input type="text" name="username"></input><br />
			<label for="password">Password</label><input type="password" name="password"></input><br />
			<label for="password2">Verify Password</label><input type="password" name="password2"></input><br />
			<label for="username">Email</label><input type="text" name="email"></input><br />
			<input type="hidden" name="make_administrator" value="true"></input>
			<input type="submit" name="submit" class="submit"  onclick="document.getElementById('submit').value = 'Creating...'" value="Create Administrator"></input>
		</form>
	</p>
<?
		}
	}

	if($_POST['wp_import'] == "true") {
		include('import_wp.php');
	}
	
	if(($_POST['make_administrator'] == "true") && ($_POST['password'] == $_POST['password2'])) {
		
		$username = $_POST['username'];
		$password = md5($_POST['[password]']);
		$email = $_POST['email'];
		$username_md5 = md5($username);
		drizzo_db_connect();
		$admin_create = mysql_query("INSERT INTO `$drizzo_users` (`username`, `password`, `email`, `username_md5`, `user_level`) VALUES ('$username', '$password', '$email', '$username_md5', '5')");
		if(!$admin_create) {
			echo "<p>Database error. User was not created.</p>" . mysql_error();
		} else {
?>
	<h2>Installation Complete</h2>
	
	<p>That's it! <a href="/drizzo">Click here to go login and get started.</a></p>
<?
		}
	}
	
	if(($_POST['make_administrator'] == "true") && ($_POST['password'] != $_POST['password2'])) {
?>
	<h2>Create Administrator Account</h2>
	
	<p>Passwords did not match. Please try again.</p>

	<p>
		<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" name="make_admin" id="installer">
			<label for="username">Username</label><input type="text" name="username" value="<? echo $_POST['username']; ?>"></input><br />
			<label for="password">Password</label><input type="password" name="password"></input><br />
			<label for="password2">Verify Password</label><input type="password" name="password2"></input><br />
			<label for="username">Email</label><input type="text" name="email" value="<? echo $_POST['email']; ?>"></input><br />
			<input type="hidden" name="make_administrator" value="true"></input>
			<input type="hidden" name="db" value="<? echo $_POST['db']; ?>"></input>
			<input type="hidden" name="db_host" value="<? echo $_POST['db_host']; ?>"></input>
			<input type="hidden" name="db_user" value="<? echo $_POST['db_user']; ?>"></input>
			<input type="hidden" name="db_pass" value="<? echo $_POST['db_pass']; ?>"></input>
			<input type="hidden" name="db_prefix" value="<? echo $_POST['db_prefix']; ?>"></input>
			<input type="submit" name="submit" class="submit"  onclick="document.getElementById('submit').value = 'Creating...'" value="Create Administrator"></input>
		</form>
	</p>
<?
	}
?>
		</div>
			<div id="footer">

				<p>[ Drizzo <? if(function_exists(drizzo_version_number)) { echo drizzo_version_number(); } ?> ] [ Licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/2.5/">Creative Commons Attribution Share-Alike License</a> ] [ Built by <a href="http://dryanmedia.com" title="dryan media">dryan media</a> ]</p>

			</div>

			</body>

			<? if (function_exists(drizzo_load_editor)) { drizzo_load_editor(); } ?>

		</html>